package com.wes.dsptest.camera;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.TextureView;

public class CameraTextureView extends TextureView implements CameraViewInterface {
    private CameraSurfaceCallback mCallback;
    private Surface mSurface;

    public CameraTextureView(Context context) {
        super(context);
        setSurfaceTextureListener(new SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                mSurface = new Surface(surface);
                if (mCallback != null) {
                    mCallback.onSurfaceCreated();
                    mCallback.onSurfaceChanged(width, height);
                }
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                if (mCallback != null) {
                    mCallback.onSurfaceChanged(width, height);
                }
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                if (mCallback != null) {
                    mCallback.onSurfaceDestroyed();
                }
                if (mSurface != null) {
                    mSurface.release();
                    mSurface = null;
                }
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                // Do nothing.
            }
        });
    }

    @Override
    public Surface getSurface() {
        return mSurface;
    }

    @Override
    public void addCallback(CameraSurfaceCallback callback) {
        mCallback = callback;
    }
}
